package com.example.opengldemo._112_;

import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.R;
import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;

public class Mountion {
    ShaderHandle shaderHandle ;
    ShaderBuffer shaderBuffer ;
    final float UNIT= 1.0f ;

    String vertexPath = "mountion/vertex.sh" ;
    String fragPath = "mountion/fragment.sh" ;
    public Mountion(View view){
        String vertexRes = ShaderUtil.loadFromAssetsFile(vertexPath , view.getResources());
        String fragRes = ShaderUtil.loadFromAssetsFile(fragPath , view.getResources());
        int program = ShaderUtil.createProgram(vertexRes , fragRes);
        shaderHandle = new ShaderHandle(program);
        shaderBuffer = new ShaderBuffer();
        float[][] hArray = Constant.loadLandforms(view.getResources() , R.mipmap.land);
        shaderBuffer.setVertexBuffer(getVertexArray(hArray));
        shaderBuffer.setTexcoorBuffer(getTexcoorsArray(hArray[0].length,hArray.length ));

        shaderHandle.UHandleArray[0] = shaderHandle.getUHandle("sTexture");
        shaderHandle.UHandleArray[1] = shaderHandle.getUHandle("sTexture1");
        shaderHandle.UHandleArray[2] = shaderHandle.getUHandle("uDh");


    }

    public float[] getVertexArray(float[][] yArray){
        int w = yArray[0].length ;
        int h = yArray.length ;
        shaderBuffer.vCount = w*h*2*3 ;
        float[] vertices = new float[shaderBuffer.vCount*3];
        int count = 0 ;
        for (int i = 0  ; i < h-1 ; i++ ){
            for (int j = 0 ; j < w-1 ;j++){
                float zsx=-UNIT*w/2+i*UNIT;
                float zsz=-UNIT*h/2+j*UNIT;
//                float zsx=i*UNIT;
//                float zsz=j*UNIT;

                vertices[count++]=zsx;
                vertices[count++]=yArray[j][i];
                vertices[count++]=zsz;

                vertices[count++]=zsx;
                vertices[count++]=yArray[j+1][i];
                vertices[count++]=zsz+UNIT;

                vertices[count++]=zsx+UNIT;
                vertices[count++]=yArray[j][i+1];
                vertices[count++]=zsz;

                vertices[count++]=zsx+UNIT;
                vertices[count++]=yArray[j][i+1];
                vertices[count++]=zsz;

                vertices[count++]=zsx;
                vertices[count++]=yArray[j+1][i];
                vertices[count++]=zsz+UNIT;

                vertices[count++]=zsx+UNIT;
                vertices[count++]=yArray[j+1][i+1];
                vertices[count++]=zsz+UNIT;

            }
        }
        return vertices ;
    }

    public float[] getTexcoorsArray(int bw , int bh){
        float[] result=new float[bw*bh*6*2];
        float sizew=10.0f/bw;
        float sizeh=10.0f/bh;
        int c=0;
        for(int i=0;i<bh-1;i++)
        {
            for(int j=0;j<bw-1;j++)
            {
                //√ø––¡–“ª∏ˆæÿ–Œ£¨”…¡Ω∏ˆ»˝Ω«–Œππ≥…£¨π≤¡˘∏ˆµ„£¨12∏ˆŒ∆¿Ì◊¯±Í
                float s=j*sizew;
                float t=i*sizeh;

                result[c++]=s;
                result[c++]=t;

                result[c++]=s;
                result[c++]=t+sizeh;

                result[c++]=s+sizew;
                result[c++]=t;

                result[c++]=s+sizew;
                result[c++]=t;

                result[c++]=s;
                result[c++]=t+sizeh;

                result[c++]=s+sizew;
                result[c++]=t+sizeh;
            }
        }
        return result;
    }

    public void drawSelf(int id , int id2){
        shaderHandle.program()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindMVPMaxtrixHandle();


        GLES30.glUniform1f(shaderHandle.UHandleArray[2] , 20f );

        GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , id);
        GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , id2);

        GLES30.glUniform1i(shaderHandle.UHandleArray[0] , 0);
        GLES30.glUniform1i(shaderHandle.UHandleArray[1] , 1);

        shaderHandle.run(GLES30.GL_TRIANGLES , shaderBuffer.vCount );
    }


}
